//
//  JYSubWebViewController.m
//  FinderBox
//
//  Created by JiYi on 2018/10/12.
//  Copyright © 2018 JiYi. All rights reserved.
//

#import "JYSubWebViewController.h"


@interface JYSubWebViewController ()<WKNavigationDelegate, WKUIDelegate>

@end

@implementation JYSubWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    self.commonWeb.UIDelegate = self;
    self.commonWeb.navigationDelegate = self;
    
    [self setupLeftBarItem];
}

- (void)setupLeftBarItem {
    if (self.navigationItem.hidesBackButton == NO) {
        JYBarButtonItem *rightBtn = [[JYBarButtonItem alloc] initContainImage:[UIImage imageNamed:@"backImage"]
                                                               imageViewFrame:CGRectMake(0, 10, 20, 20)
                                                                  buttonTitle:nil
                                                                   titleColor:nil
                                                                   titleFrame:CGRectZero
                                                                  buttonFrame:CGRectMake(0, 0, 40, 40)
                                                                       target:self
                                                                       action:@selector(back)];
        if ([self.commonWeb canGoBack]) {
            self.navigationItem.leftBarButtonItems = @[rightBtn];
        }else {
            self.navigationItem.leftBarButtonItem = rightBtn;
        }
    }
}

#pragma mark - 后退方法
- (void)back {
    if ([self.commonWeb canGoBack]) {
        [self.commonWeb goBack];
    }else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - 关闭

#pragma mark - webView方法
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    if (self.title.length == 0) {
        self.navigationItem.title = self.commonWeb.title;
    }
    [self setupLeftBarItem];
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    if ([navigationAction.request.URL.scheme  isEqual: @"tel"]) {
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{} completionHandler:^(BOOL success) {
            ;
        }];
        decisionHandler(WKNavigationActionPolicyCancel);
    }else if ([navigationAction.request.URL.absoluteString containsString:@"//itunes.apple.com"]   ) {
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{} completionHandler:^(BOOL success) {
            ;
        }];
        decisionHandler(WKNavigationActionPolicyCancel);
    }else if ([navigationAction.request.URL.absoluteString containsString:@".apk"]   ) {
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{} completionHandler:^(BOOL success) {
            ;
        }];
        decisionHandler(WKNavigationActionPolicyCancel);
    }else if ([navigationAction.request.URL.absoluteString containsString:@".ipa"]   ) {
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{} completionHandler:^(BOOL success) {
            ;
        }];
        decisionHandler(WKNavigationActionPolicyCancel);
    }else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler();
    }]];
    [self presentViewController:alertController animated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
